package com.cloudmusic.service.impl;

import java.io.*;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.cloudmusic.dao.SelectMapper;
import com.cloudmusic.entity.User;
import com.cloudmusic.service.SelectService;

@Service
@Transactional
public class SelectServiceimpl implements SelectService{

    private Logger log=Logger.getLogger(this.getClass());

	@Autowired
	SelectMapper selectMapper;
	
	public User selectUser(String username) {
		// TODO Auto-generated method stub
		User admin = selectMapper.selectUser(username);		
		return admin;
	}
	
	public JSONObject selectAll() {
		// TODO Auto-generated method stub
		List list = selectMapper.selectAll();
		JSONObject jsonObject = new JSONObject();
        for (int i = 0;i<list.size();i++){
            //获取歌词文本
            Map map = (Map) list.get(i);
            try{
                File lrcfile = new File((String) map.get("lrc"));
                InputStream ins = new FileInputStream(lrcfile);
                InputStreamReader insr = new InputStreamReader(ins,"UTF-8");
                BufferedReader reader = new BufferedReader(insr);
                String line = null;
                String lrc = "";
                while ((line = reader.readLine()) != null) {
                    lrc = lrc + line +"\n";
                }
                ((Map) list.get(i)).put("key",lrc);
            }catch (Exception e){
                log.info("歌词信息获取错误，该歌曲无歌词信息");
            }

        }
		jsonObject.put("rows", list);
		return jsonObject;
	}

    public JSONObject selectLoginAll(String uid) {
        // TODO Auto-generated method stub
        List list = selectMapper.selectAll();
        for (int i = 0;i<list.size();i++){
            //增加喜爱标识
            Map map = (Map) list.get(i);
            String sid = (String) map.get("sid");
            int islove = selectMapper.checkMusicLove(uid,sid);
            ((Map) list.get(i)).put("islove",islove);
            //获取歌词文本
            try{
                File lrcfile = new File((String) map.get("lrc"));
                InputStream ins = new FileInputStream(lrcfile);
                InputStreamReader insr = new InputStreamReader(ins,"UTF-8");
                BufferedReader reader = new BufferedReader(insr);
                String line = null;
                String lrc = "";
                while ((line = reader.readLine()) != null) {
                    lrc = lrc + line +"\n";
                }
                ((Map) list.get(i)).put("key",lrc);
            }catch (Exception e){
                log.info("歌词信息获取错误，该歌曲无歌词信息");
            }

        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", list);
        return jsonObject;
    }
	
	public JSONObject selectLike(String uid) {
		// TODO Auto-generated method stub
		List list = selectMapper.selectLike(uid);
//		获取歌曲是否喜欢状态，为添加爱心做准备
        //遍历list
        for (int i = 0;i<list.size();i++){
            Map map = (Map) list.get(i);
            String sid = (String) map.get("sid");
            int islove = selectMapper.checkMusicLove(uid,sid);
            ((Map) list.get(i)).put("islove",islove);
            //获取歌词文本
            try{
                File lrcfile = new File((String) map.get("lrc"));
                InputStream ins = new FileInputStream(lrcfile);
                InputStreamReader insr = new InputStreamReader(ins,"UTF-8");
                BufferedReader reader = new BufferedReader(insr);
                String line = null;
                String lrc = "";
                while ((line = reader.readLine()) != null) {
                    lrc = lrc + line +"\n";
                }
                ((Map) list.get(i)).put("key",lrc);
            }catch (Exception e){
                log.info("歌词信息获取错误，该歌曲无歌词信息");
            }
        }
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("rows", list);
		return jsonObject;
	}

	public JSONObject selectCollection(String uid) {
		// TODO Auto-generated method stub
		List list = selectMapper.selectCollection(uid);
        for (int i = 0;i<list.size();i++){
            Map map = (Map) list.get(i);
            String sid = (String) map.get("sid");
            int islove = selectMapper.checkMusicLove(uid,sid);
            ((Map) list.get(i)).put("islove",islove);
            //获取歌词文本
            try{
                File lrcfile = new File((String) map.get("lrc"));
                InputStream ins = new FileInputStream(lrcfile);
                InputStreamReader insr = new InputStreamReader(ins,"UTF-8");
                BufferedReader reader = new BufferedReader(insr);
                String line = null;
                String lrc = "";
                while ((line = reader.readLine()) != null) {
                    lrc = lrc + line +"\n";
                }
                ((Map) list.get(i)).put("key",lrc);
            }catch (Exception e){
                log.info("歌词信息获取错误，该歌曲无歌词信息");
            }
        }
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("rows", list);
		return jsonObject;
	}


	public JSONObject selectHistory(String historylid,String uid) {
		// TODO Auto-generated method stub
		List list = selectMapper.selectHistory(historylid);
        for (int i = 0;i<list.size();i++){
            Map map = (Map) list.get(i);
            String sid = (String) map.get("sid");
            int islove = selectMapper.checkMusicLove(uid,sid);
            ((Map) list.get(i)).put("islove",islove);
            //获取歌词文本
            try{
                File lrcfile = new File((String) map.get("lrc"));
                InputStream ins = new FileInputStream(lrcfile);
                InputStreamReader insr = new InputStreamReader(ins,"UTF-8");
                BufferedReader reader = new BufferedReader(insr);
                String line = null;
                String lrc = "";
                while ((line = reader.readLine()) != null) {
                    lrc = lrc + line +"\n";
                }
                ((Map) list.get(i)).put("key",lrc);
            }catch (Exception e){
                log.info("歌词信息获取错误，该歌曲无歌词信息");
            }
        }
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("rows", list);
		return jsonObject;
	}

}
